package com.example.springthymeleafproject.controller;


import com.example.springthymeleafproject.model.Department;
import com.example.springthymeleafproject.model.Device;
import com.example.springthymeleafproject.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;


//    @GetMapping("/department")
//    public String viewDepartmentPage(Model model){
//        model.addAttribute("listDepartment", departmentService.getAllDepartment());
//        return "department";
//    }

    //部门分页主页面
    @GetMapping("/department")
    //默认以id作为排序列，且为降序
    public String viewHomePage(Model model) {
        return findDepartmentPaginated(1, "id", "asc", model);
    }

    // 显示添加部门页面
    @GetMapping("/addDepartment")
    public String addDepartmentForm(Model model) {
        //创建模型属性来绑定表单数据
        Department department = new Department();
        model.addAttribute("department", department);
        return "addDepartment";
    }
    //添加部门
    @PostMapping("/saveDepartment")
    public String saveDepartment(@ModelAttribute("device") Department department){
        //提交保存数据
        departmentService.saveDepartment(department);
        return "redirect:/department";
    }

    //更新部门信息
    @GetMapping("/updateDepartment/{id}")
    public String UpdateDevice(@PathVariable(value = "id") long id, Model model){
        //从服务中获取设备
        Department department = departmentService.getDepartmentId(id);

        //将device设置为模型属性以预填充表单
        model.addAttribute("department", department);
        model.addAttribute("departments", departmentService.getAllDepartment());

        return "updateDepartment";
    }

    //获取分页数据并对通过字段排序排序
    @GetMapping("/department/page/{pageNo}")
    public String findDepartmentPaginated(
            // value 指定了绑定的路径参数名称，int 表示将路径参数转成整型类型并绑定到 pageNo 参数上。
            @PathVariable(value = "pageNo") int pageNo,
            //@RequestParam 注解表示这个参数是一个查询参数，用来指定排序字段和排序方向。其中：
            //"sortField" 表示查询参数的名称，也就是排序的字段。
            //"sortDir" 表示查询参数的名称，也就是排序的方向。（升序|降序）
            @RequestParam("sortField") String sortField,
            @RequestParam("sortDir") String sortDir,
            Model model
    ) {
        //页面中放几个数据
        int pageSize = 7;
        //employeeService.findPaginated(pageNo, pageSize, sortField, sortDir)
        // 方法通过传递的参数 pageNo、pageSize、sortField 和 sortDir 来实现分页查询，并返回一个 Page<Employee> 对象，
        // 其中包含指定 '页码和数量' 的员工数据。
        Page<Department> page = departmentService.findDepartmentPaginated(pageNo, pageSize, sortField, sortDir);
        //page.getContent() 方法可以获取到当前页的员工列表，然后将分页和排序相关的数据和员工列表数据传递到前端视图层页面。
        List<Department> listDepartment = page.getContent();

        model.addAttribute("currentPage", pageNo);
        model.addAttribute("totalPages", page.getTotalPages());
        model.addAttribute("totalItems", page.getTotalElements());


        model.addAttribute("sortField", sortField);
        model.addAttribute("sortDir", sortDir);
        model.addAttribute("reverseSortDir", sortDir.equals("asc") ? "desc" : "asc");

        model.addAttribute("listDepartment", listDepartment);
        return "department";
    }


}
